package com.example.rabbit.three;

import com.example.rabbit.util.RabbitMqUtils;
import com.example.rabbit.util.SleepUtil;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 队列模式
 * 1.不自动应答
 * 2.不公平分发
 **/
public class Work022 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();

        System.out.println("工作线程C2等待。。。");

        DeliverCallback deliverCallback = (consumeTag, deliver) -> {
            SleepUtil.sleep(30);
            System.out.println("收到消息" + consumeTag + "=====" + new String(deliver.getBody(), StandardCharsets.UTF_8));
            channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = consumeTag -> {
            System.out.println("取消应答" + consumeTag);
        };

        // 不公平分发  预取值
        channel.basicQos(5);
        String queueName = "ack_queue";
        boolean autoAsk = false;
        channel.basicConsume(queueName, autoAsk, deliverCallback, cancelCallback);
    }
}
